<% extends "common/msui.html" %>
<% block title %>地址管理<% endblock %>
<% block body %>
<div class="page" id="address" ms-controller="address">
  <header class="bar bar-nav">
    <!-- <a class='button button-link button-nav pull-right' style='margin-right:5px' href='/mshop/customer/address/edit?partner_id=${partner_id}&address_id=0' ><span>新增</span></a> -->
    <a class="button button-link button-nav pull-left back" href="/mshop/products" >
      <span class="icon icon-left"></span>
      返回
    </a>
    <h1 class="title">地址管理</h1>
  </header>

  <div class="content" >
      <div class="card multi-address" ms-repeat='address' ms-visible='!el.unlink'>
        <div class="card-content multi-address-content" >
          <div><span>{{el.name}} {{el.phone}}</span> <span ms-visible='el.default' >默认地址</span></div>
          <div><span>{{el.province}}{{el.city}}{{el.country}} {{el.street}}</span></div>
        </div>
        <div class="card-footer multi-address-footer">
            <div style='margin-left:5px;margin-right:10px' ms-click='default_line(el)' >
              <div class='address-circle-choose' ms-class='address-circle-choosed:el.default'></div>
              <span>设为默认地址</span>
            </div>

            <div ><span><a ms-attr-href='/mshop/customer/address/edit?partner_id={{partner_id}}&address_id={{el.id}}' class='link open-indicator'>编辑</a></span><span ms-click='delete_customer(el)'>删除</span></div>
        </div>
      </div>

  </div>

  <footer>
    <nav class="bar bar-tab index-footer">
      <a class="tab-item" href="/mshop?tab=index">
        <span class="icon icon-home"></span>
        <span class="tab-label">首页</span>
      </a>
      <a class="tab-item" href="/mshop?tab=cart">
        <span class="icon icon-cart"></span>
        <span class="tab-label">购物车</span>
      </a>
      <a class="tab-item" href="/mshop?tab=my">
        <span class="icon icon-me"></span>
        <span class="tab-label">我的</span>
      </a>
    </nav>
  </footer>

  <script type="application/javascript" >
    $(function() {
        var partner_id = ${ partner_id | safe }

        var sync_content = function() {
            $.ajax({
                url: '/mshop/customer/address/get_multi',
                data: {
                    partner_id: partner_id
                },
                type: 'post',
                success: function(e) {
                    address_vm.address = JSON.parse(e);
                },
                async: true,
            });
        }

        sync_content()

        var address_vm = avalon.define({
            $id: 'address',
            address: [],
            partner_id: partner_id,
            default_line: function(line) {
                if (!line.default) {
                    for (var index in address_vm.address) {
                        address_vm.address[index].default = false;
                    };

                    line.default = true;
                    $.ajax({
                        url: '/mshop/customer/address/update_default',
                        data: {
                            address_id: line.id
                        },
                        type: 'post',
                        async: true
                    });
                };

            },
            delete_customer: function(line) {
                $.confirm('确认要删除此收货地址吗？', function() {
                    line.unlink = true;
                    $.ajax({
                        url: '/mshop/customer/address/unlink',
                        data: {
                            address_id: line.id
                        },
                        type: 'post',
                        async: true
                    });
                });
            },
        })

        $(document).on('pageReinit', '#address', function(e, pageId, $page) {
            console.log('13');
            sync_content();
        });

        avalon.scan();
    });


  </script>

<% endblock %>
</div>
